package hk.cic.cwrs.bio.integration;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.provider.Settings;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;
import com.mjzuo.location.bean.Latlng;
import com.mjzuo.location.location.IGeocoding;
import com.mjzuo.location.location.OpenCellidGeocoding;

import java.util.Iterator;
import java.util.List;

import hk.cic.cwrs.bio.R;
import hk.cic.cwrs.bio.integration.log.IBioService;

public class MainActivity extends BaseActivity {

    private LocationManager lm;
    private String TAG="data";
    private IBioService iOperationManager;
    private boolean isConnet=false;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iOperationManager= IBioService.Stub.asInterface(service);
            Log.e(TAG,"true");
            isConnet=true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            iOperationManager = null;
            Log.e(TAG,"false");
            isConnet=false;
        }
    };


    @Override
    protected int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        bindService();
    }

    @Override
    protected void initUI() {
        Log.e(TAG,"onCreate");
        bindService();
        XXPermissions.with(this)
                // 可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                .constantRequest()
                // 支持请求6.0悬浮窗权限8.0请求安装权限
                //.permission(Permission.SYSTEM_ALERT_WINDOW, Permission.REQUEST_INSTALL_PACKAGES)
                // 不指定权限则自动获取清单中的危险权限
                .request(new OnPermission() {

                    @SuppressLint("MissingPermission")
                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        if (isAll) {

                        }
//                        OpenCellidGeocoding openCellidGeocoding  =new OpenCellidGeocoding(context);
//                        openCellidGeocoding.start(new IGeocoding.ISiLoResponseListener() {
//                            @Override
//                            public void onSuccess(Latlng latlng) {
//                                Log.i("aa",latlng.getLatitude()+"");

//                            }
//
//                            @Override
//                            public void onFail(String msg) {
//                                Log.i("aa",msg);

//                            }
//                        });
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {

                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {
        Button viewById = (Button) findViewById(R.id.btn);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isConnet){
                    try {
                        iOperationManager.ResultLog("啊啊啊");
                        ActivityTool.getInstance().popOneActivity(MainActivity.this);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    private void bindService() {
        Intent intent = new Intent();
        intent.setClassName("hk.cic.cwrs.bio.integration", "hk.cic.cwrs.bio.integration.MyService");
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (serviceConnection != null) {
            unbindService(serviceConnection);
        }
    }
}